<template>
  <div>
    <el-dialog
      :title="sysBusinessDetectionItem.unit"
      align="center"
      class="base-dialog readonly"
      :custom-class="'panna-dialog_medium'"
      :visible.sync="unitVisible"
      :before-close="closeDialog"
      :append-to-body="true"
      @closed="closeDialog"
    >
      <el-form ref="filterForm" :model="filterForm" inline>
        <el-form-item :label="sysBusinessDetectionItem.unit" prop="orderNum">
          <el-input
            v-model="filterForm.unitName"
            :placeholder="sysBusinessDetectionItem.unit"
            clearable
          />
        </el-form-item>
        <el-form-item class="base-filter-button">
          <el-button type="primary" @click="select">{{
            operateType.query
          }}</el-button>
          <el-button @click="cancelForm">{{ operateType.clear }}</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        height="340px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          align="center"
          :label="sysBusinessDetectionItem.serialNumber"
          width="50"
        >
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column :label="sysBusinessDetectionItem.unit">
          <template slot-scope="scope">
            <a style="color: blue" @click="handleSelectionChange(scope.row)">{{
              scope.row.unitName
            }}</a>
          </template>
        </el-table-column>
      </el-table>
      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getList } from "../../../../api/SysBusiness/SysBusinessUnit.js";
export default {
  name: "ChoiceUnit",
  props: {
    unitVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      filterForm: {
        unitName: "",
      },
      loading: false,
      list: [],
      // 复选框选中数据
      lists: [],
      formLabelWidth: "200px",
      choiceData: {},
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    sysBusinessDetectionItem() {
      return this.$t("SysBusinessDetectionItem") || {};
    },
  },
  created() {
    this.getList();
  },
  methods: {
    getList(flag) {
      if (flag) {
        this.pagination.pageIndex = 1;
      }
      getList({
        ...this.filterForm,
        ...this.pagination,
      })
        .then((res) => {
          // 分页序号连续
          this.list = res.data.dataList;
          this.pagination.total = res.data.total;
        })
        .catch(() => {
          this.list = [];
        });
    },
    cancelForm() {
      this.filterForm.unitName = "";
      this.sizeChange(10);
    },
    select() {
      this.sizeChange(10);
    },
    cancel() {
      this.$emit("update:unitVisible", false);
    },
    closeDialog() {
      this.cancel();
    },
    handleSelectionChange(row) {
      this.$emit("setUnit", row);
      this.closeDialog();
    },
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
  },
};
</script>
